package com.example.timeplanning_teacher;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {

    private SharedPreferences loginPreference;
    String username;
    String password;
    Toast timeOut_Toast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        timeOut_Toast=Toast.makeText(SplashActivity.this,"网络连接超时",Toast.LENGTH_SHORT);

        loginPreference = getSharedPreferences("parent", Context.MODE_PRIVATE);
        username = loginPreference.getString("username","");
        password = loginPreference.getString("password","");
        init();

    }

    private void init() {
        //利用timer让此界面延迟3秒后跳转，timer有一个线程，该线程不断执行task
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                LoginThread loginThread = new LoginThread();
                loginThread.start();
                overridePendingTransition(R.anim.trans_in, R.anim.trans_out);
            }
        };
        //调度执行timerTask，第二个参数传入延迟时间（毫秒）
        timer.schedule(timerTask,3000);
    }

    class LoginThread extends Thread{
        @Override
        public void run() {
            try {
                String json = "{\n" +
                        "  \"username\": \"" + username + "\",\n" +
                        "  \"password\": \"" + password + "\"\n" +
                        "}";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://58.58.36.198:9900/parent/login")
                        .post(RequestBody.create(MediaType.parse("application/json"),json))
                        .build();
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                JSONObject jsonObject=new JSONObject(responseData);
                int code=(int)jsonObject.get("code");
                if(code == 100){
                    Intent intent =new Intent(SplashActivity.this, FragmentActivity.class);
                    startActivity(intent);
                    SplashActivity.this.finish();
                }
                else{
                    Intent intent =new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    SplashActivity.this.finish();
                }
            }catch (Exception e){
                e.printStackTrace();
                timeOut_Toast.show();
                Intent intent =new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                SplashActivity.this.finish();
            }
        }
    }

}